package com.example.consumptionrecords.service.impl;

import com.example.consumptionrecords.entity.dto.ConRecordsDTO;
import com.example.consumptionrecords.entity.vo.ConRecordsStatisticsVO;
import com.example.consumptionrecords.entity.pojo.*;
import com.example.consumptionrecords.entity.vo.HomeCardVO;
import com.example.consumptionrecords.mapper.HomeMapper;
import com.example.consumptionrecords.service.interfaces.HomeService;
import com.example.consumptionrecords.util.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * SunJingxuan
 * 2022 \ 07 \ 29
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

	@Resource
	private HomeMapper homeMapper;

	@Override
	public List<HomeCardVO> getCard(Integer userId) {
		// 首页卡片
		List<HomeCard> homeCards = homeMapper.selectHomeCard();
		List<HomeCardVO> homeCardVOList = new ArrayList<>();
		homeCards.forEach(card -> {
			HomeCardVO homeCardVO = new HomeCardVO();
			BeanUtils.copyProperties(card, homeCardVO);
			// 0、day 1、month 2、seasons 3、year
			switch (card.getType()) {
				case 0:
//					BigDecimal dayTotalAmount = conRecordsStatistics.stream().map(ConRecordsStatistics::getConAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
					// 查询今天的消费记录
					List<ConRecordsStatisticsVO> conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getDayStart(), DateUtil.getDayEnd(), userId);
					final BigDecimal[] dayTotalAmount = {BigDecimal.ZERO};
					StringBuffer dayContent = new StringBuffer();
					// 累加消费金额，并拼接消费类型
					conRecordsStatistics.forEach(con -> {
						dayTotalAmount[0] = dayTotalAmount[0].add(con.getConAmount());
						dayContent.append(con.getConType()).append(":").append(con.getConAmount()).append("  ");
					});
					homeCardVO.setContent(dayContent.toString());
					homeCardVO.setTotalMoney(dayTotalAmount[0].toPlainString());
					break;
				case 1:
					// 查询当月消费记录
					List<ConRecordsStatisticsVO> conRecordsStatistics1 = homeMapper.selectConRecordsStatistics(DateUtil.getMonthFirstDay(), DateUtil.getMonthLastDay(), userId);
					final BigDecimal[] dayTotalAmount1 = {BigDecimal.ZERO};
					StringBuffer dayContent1 = new StringBuffer();
					// 累加消费金额，并拼接消费类型
					conRecordsStatistics1.forEach(con -> {
						dayTotalAmount1[0] = dayTotalAmount1[0].add(con.getConAmount());
						dayContent1.append(con.getConType()).append(":").append(con.getConAmount()).append("  ");
					});
					homeCardVO.setContent(dayContent1.toString());
					homeCardVO.setTotalMoney(dayTotalAmount1[0].toPlainString());
					break;
				case 2:
					List<ConRecordsStatisticsVO> conRecordsStatistics2 = new ArrayList<>();
					// 根据季度查询消费记录（季度按照四个节气划分）
					switch (DateUtil.getSeasonsInt()) {
						case 1 ->
								conRecordsStatistics2 = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-03-21", DateUtil.getYear() + "-06-20", userId);
						case 2 ->
								conRecordsStatistics2 = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-06-21", DateUtil.getYear() + "-09-20", userId);
						case 3 ->
								conRecordsStatistics2 = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-09-21", DateUtil.getYear() + "-12-20", userId);
						case 4 ->
								conRecordsStatistics2 = homeMapper.selectConRecordsStatistics(DateUtil.getYear() - 1 + "-12-21", DateUtil.getYear() + "-03-20", userId);
						default -> {}
					}
					final BigDecimal[] dayTotalAmount2 = {BigDecimal.ZERO};
					StringBuffer dayContent2 = new StringBuffer();
					// 累加消费金额，并拼接消费类型
					conRecordsStatistics2.forEach(con -> {
						dayTotalAmount2[0] = dayTotalAmount2[0].add(con.getConAmount());
						dayContent2.append(con.getConType()).append(":").append(con.getConAmount()).append("  ");
					});
					homeCardVO.setContent(dayContent2.toString());
					homeCardVO.setTotalMoney(dayTotalAmount2[0].toPlainString());
					break;
				case 3:
					// 查询当年消费记录
					List<ConRecordsStatisticsVO> conRecordsStatistics3 = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-01-1", DateUtil.getYear() + "-12-31", userId);
					final BigDecimal[] dayTotalAmount3 = {BigDecimal.ZERO};
					StringBuffer dayContent3 = new StringBuffer();
					// 累加消费金额，并拼接消费类型
					conRecordsStatistics3.forEach(con -> {
						dayTotalAmount3[0] = dayTotalAmount3[0].add(con.getConAmount());
						dayContent3.append(con.getConType()).append(":").append(con.getConAmount()).append("  ");
					});
					homeCardVO.setContent(dayContent3.toString());
					homeCardVO.setTotalMoney(dayTotalAmount3[0].toPlainString());
					break;
			}
			BigDecimal money = new BigDecimal(homeCardVO.getTotalMoney());
			homeCardVO.setTarget(money.divide(BigDecimal.valueOf(100000000)).toPlainString());
			homeCardVOList.add(homeCardVO);
		});
		return homeCardVOList;
	}

	@Override
	public List<ConRecordsStatisticsVO> selectConRecordsStatistics(Integer dateType, Integer userId) {
		List<ConRecordsStatisticsVO> conRecordsStatistics = new ArrayList<>();
		// 0、day 1、month 2、seasons 3、year
		switch (dateType) {
			case 0:
				// 查询当天消费记录
				conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getDayStart(), DateUtil.getDayEnd(), userId);
				break;
			case 1:
				// 查询当月消费记录
				conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getMonthFirstDay(), DateUtil.getMonthLastDay(), userId);
				break;
			case 2:
				// 根据季度查询消费记录（季度按照四个节气划分）
				switch (DateUtil.getSeasonsInt()) {
					case 1 ->
							conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-03-21", DateUtil.getYear() + "-06-20", userId);
					case 2 ->
							conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-06-21", DateUtil.getYear() + "-09-20", userId);
					case 3 ->
							conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-09-21", DateUtil.getYear() + "-12-20", userId);
					case 4 ->
							conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getYear() - 1 + "-12-21", DateUtil.getYear() + "-03-20", userId);
					default -> {}
				}
				break;
			case 3:
				// 查询当年消费记录
				conRecordsStatistics = homeMapper.selectConRecordsStatistics(DateUtil.getYear() + "-01-01", DateUtil.getYear() + "-12-31", userId);
				break;
		}
		// 拼接消费类型和金额
		if (!CollectionUtils.isEmpty(conRecordsStatistics)) {
			conRecordsStatistics.forEach(con -> {
				con.setLabelText(con.getConType() + ":" + con.getConAmount() + "元");
			});
			return conRecordsStatistics;
		}
		return new ArrayList<>();
	}

	@Override
	public List<ConPayChannel> selectConPayChannel() {
		return homeMapper.selectConPayChannel();
	}

	@Override
	public List<ConPlatform> selectConPlatform() {
		return homeMapper.selectConPlatform();
	}

	@Override
	public List<ConType> selectConType() {
		return homeMapper.selectConType();
	}

	@Override
	public void insertConRecords(ConRecordsDTO conRecordsDTO, Integer userId) {
		homeMapper.insertConRecords(conRecordsDTO, userId);
	}


}
